package com.cool.feiyan.ui.dashboard

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.net.Uri
import android.os.Bundle
import android.os.Parcel
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.style.URLSpan
import android.text.style.UnderlineSpan
import android.util.Log
import android.view.LayoutInflater
import android.view.OrientationEventListener
import android.view.View
import android.view.ViewGroup
import android.widget.Adapter
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.core.text.toSpannable
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cool.feiyan.FeiYanData
import com.cool.feiyan.MainActivity
import com.cool.feiyan.R
import java.util.jar.Manifest

class DashboardFragment : Fragment() {

    private lateinit var dashboardViewModel: DashboardViewModel
    private lateinit var dashboardFragment: View

    private lateinit var recyclerView: RecyclerView
    private lateinit var viewAdapter: RecyclerView.Adapter<*>
    private lateinit var viewManager: RecyclerView.LayoutManager

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dashboardViewModel =
            ViewModelProviders.of(this).get(DashboardViewModel::class.java)

        dashboardFragment = inflater.inflate(R.layout.fragment_dashboard, container, false)
        val textView: TextView = dashboardFragment.findViewById(R.id.text_dashboard)
        dashboardViewModel.text.observe(this, Observer {
            textView.text = it
        })

        viewManager = LinearLayoutManager(dashboardFragment.context)
        (viewManager as LinearLayoutManager).orientation = LinearLayoutManager.VERTICAL
        (viewManager as LinearLayoutManager).isSmoothScrollbarEnabled = true
        viewAdapter = FeiYanAdpater((activity as MainActivity).resultList ) // Is this the best practice?
        recyclerView = dashboardFragment.findViewById<RecyclerView>(R.id.recycler_view_content_display).apply {

            // use this setting to improve performance if you know that changes
            // in content do not change the layout size of the RecyclerView
            setHasFixedSize(true)

            // use a linear layout manager
            layoutManager = viewManager

            // specify an viewAdapter (see also next example)
            adapter = viewAdapter
        }

        return dashboardFragment
    }

    class FeiYanAdpater(private val dataList : MutableList<FeiYanData>) :
            RecyclerView.Adapter<FeiYanAdpater.FeiYanViewHolder>() {

        // Provide a reference to the views for each data item
        // Complex date items may need more than one view per item, and
        // you provide access to all the views for a data item in a view holder.
        // Each data item is just a list of strings in this case that is shown in a CardView
        class FeiYanViewHolder(val cardView : CardView) : RecyclerView.ViewHolder(cardView)

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeiYanAdpater.FeiYanViewHolder {
            // create a new view
            val cardView = LayoutInflater.from(parent.context).inflate(R.layout.fragment_card_view, parent,false) as CardView
            return FeiYanViewHolder(cardView)
        }

        override fun onBindViewHolder(holder: FeiYanViewHolder, position: Int) {
            val 交通类型 : TextView = holder.cardView.findViewById(R.id.text_view_交通类型)
            val 日期 : TextView = holder.cardView.findViewById(R.id.text_view_日期)
            val 车次_车牌_航班号_场所名 : TextView = holder.cardView.findViewById(R.id.text_view_车次_车牌_航班号_场所名)
            val 车厢 : TextView = holder.cardView.findViewById(R.id.text_view_车厢)
            val 出发站 : TextView = holder.cardView.findViewById(R.id.text_view_出发站)
            val 到达站  : TextView = holder.cardView.findViewById(R.id.text_view_到达站)
            val 车次附加描述 : TextView = holder.cardView.findViewById(R.id.text_view_车次附加描述)
            val 开始时间 : TextView = holder.cardView.findViewById(R.id.text_view_开始时间)
            val 结束时间 : TextView = holder.cardView.findViewById(R.id.text_view_结束时间)
            val 线索来源名称 : TextView = holder.cardView.findViewById(R.id.text_view_线索来源名称)
            //val 线索来源网址 : TextView = holder.cardView.findViewById(R.id.text_view_线索来源网址)
            val 提交时间 : TextView = holder.cardView.findViewById(R.id.text_view_提交时间)
            val 更新时间 : TextView = holder.cardView.findViewById(R.id.text_view_更新时间)
            val 验证通过 : TextView = holder.cardView.findViewById(R.id.text_view_验证通过)

            交通类型.text = dataList[position].交通类型
            日期.text = dataList[position].日期
            车次_车牌_航班号_场所名.text = dataList[position].车次_车牌_航班号_场所名
            车厢.text = dataList[position].车厢
            出发站.text = dataList[position].出发站
            到达站.text = dataList[position].到达站
            车次附加描述.text = dataList[position].车次附加描述
            开始时间.text = dataList[position].开始时间
            结束时间.text = dataList[position].结束时间
            线索来源名称.text = dataList[position].线索来源名称
            //线索来源网址.text = dataList[position].线索来源网址
            提交时间.text = dataList[position].提交时间
            更新时间.text = dataList[position].更新时间

            var 来源 : SpannableString = SpannableString(dataList[position].线索来源名称)
            var underlineSpan : ColorUnderlineSpan = ColorUnderlineSpan(dataList[position].线索来源网址, Color.parseColor("#00B2EE"))
            来源.setSpan(underlineSpan, 0, 来源.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            线索来源名称.text = 来源

            线索来源名称.setOnClickListener { view: View ->
                underlineSpan.onClick(view)
            }

            来源 = SpannableString(dataList[position].线索来源网址)
            来源.setSpan(underlineSpan, 0, 来源.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            /*
            线索来源网址.text = 来源

            线索来源网址.setOnClickListener { view: View ->
                underlineSpan.onClick(view)
            }
           */
            val page : String = (position + 1).toString() + "/" + dataList.size.toString()
            if(dataList[position].验证通过)
              验证通过.text = "是        " + page
            else
                验证通过.text = "否         " + page
        }

        override fun getItemCount(): Int = dataList.size
    }

    class ColorUnderlineSpan : URLSpan {

        private val _color : Int
        private val _uri : String

        constructor(uri : String, color : Int) : super(uri) {
            _color = color
            _uri = uri
        }

        override fun updateDrawState(ds : TextPaint) {
            super.updateDrawState(ds)
            ds.setColor(_color)
        }

        // 不知道什为么这个 onClick 方法没有触发 whg 2020-02-05
        override fun onClick(widget: View) {
            Log.d("onClick", "点击了")
            super.onClick(widget)
            val intent : Intent = Intent(Intent.ACTION_VIEW, Uri.parse(_uri))
            widget.context.startActivity(intent)
        }
    }
}